<template>
    <view class="content" :style="{ paddingBottom: (acceptOrderList.length > 2) ? '200rpx' : '0' }">
        <view class="list" v-if="acceptOrderList.length > 0">
            <view class="order_card mb24" @click="toDetial(item)" v-for="(item, index) in acceptOrderList" :key="index">
                <view class="order_card_head flex_b">
                    <view class="order_card_head_l flex_l">
                        <view style="font-weight: 600;">预约订单</view>
                    </view>
                    <!-- 订单状态 进行中 -->
                    <view v-if="item.status == 2" class="order_card_head_r" style="color: #FE6E04;">倒计时：{{
                        getTime(item.workDate) }}</view>
                    <!-- 订单状态 已完成 -->
                    <view v-if="item.status == 3" class="order_card_head_r" style="color: #888888;;">订单完成</view>
                </view>
                <view class="order_card_content">
                    <view class="order_id">
                        订单编号: {{ item.id }}
                    </view>
                    <view class="work">
                        <view class="_icon">
                            <image src="@/static/images/customerPage/icon_work.png" mode="scaleToFill" />
                        </view>
                        <view class="_text">{{ item.workType }}</view>
                    </view>
                    <view class="address">
                        <view class="_icon">
                            <image src="@/static/images/customerPage/icon_address.png" mode="scaleToFill" />
                        </view>
                        <view class="_text">{{ item.workPlace }}</view>
                    </view>
                    <view class="time flex_b">
                        <view class="t_l">
                            <view class="_icon">
                                <image src="@/static/images/customerPage/icon_time.png" mode="scaleToFill" />
                            </view>
                            <view class="_text">{{ item.workDate }}</view>
                        </view>
                        <view class="t_r">
                            <view>报价</view>
                            <view class="money">¥{{ item.workMoney }}起</view>
                        </view>
                    </view>
                </view>
                <view class="order_card_foot flex_b">
                    <view class="order_card_foot_l flex_l" @click.stop="handlerCancelOrder(item)">
                        {{ item.status == 2 ? '取消订单' : ' '}}
                    </view>
                    <view class="order_card_foot_r flex_c">查看详情</view>
                </view>
            </view>
        </view>
        <up-empty mode="order" marginTop="150" v-else></up-empty>
    </view>
</template>

<script setup lang="ts">
/*
* 已接单订单状态说明
* 已接单 按钮显示为查看详情
*   1 进行中======>显示倒计时
*   2 已完成=====> 已完成
*   3 取消订单 ====> 取消订单
*/
import { getTimeDiff, getTimeStamp } from '@/utils'
import { cancelOrder } from '@/api/workerPage/order'
import type { OrderParams } from '@/types/order';
const emit = defineEmits(['cancelOrder'])
defineProps<{
    acceptOrderList: OrderParams[]
}>()
// 跳转订单详情
const toDetial = ((item: any) => {
    uni.navigateTo({
        url: `/pages/workerPage/myOrder/myOrder?status=1&id=${item.id}`
    })
})
// 取消订单
const handlerCancelOrder = async (item: any) => {
    if(item.status !== 2) return
    uni.showModal({
        content: '确定取消订单？',
        success: (res) => {
            if (res.confirm) {
                cancel(item)
            }
        }
    })
}
const cancel = (async (item: any) => {
    const res: any = await cancelOrder({
        id: item.id
    })
    if (res.code == 0) {
        uni.showToast({
            title: '取消成功'
        })
        emit("cancelOrder")
    }
})

// 计算时间差
const getTime = (time: any) => {
    const startTime = new Date().getTime()
    const endTime = getTimeStamp(time)
    return getTimeDiff(startTime, endTime)
}
</script>

<style lang="scss" scoped>
.content {
    width: 100%;
}

.list {
    width: 698rpx;
    margin: 0 auto;
    margin-top: 24rpx;
}

.order_card {
    width: 698rpx;
    background: #FFFFFF;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    padding: 0 24rpx;
    box-sizing: border-box;
}

.order_card_head {
    width: 100%;
    height: 94rpx;
    border-bottom: 2rpx solid #F8F8F8;

    &_l {
        font-weight: 500;
        font-size: 30rpx;
        color: #222222;

        .distance {
            width: fit-content;
            height: 46rpx;
            padding: 0 14rpx;
            background: #FDEDEE;
            border-radius: 8rpx 8rpx 8rpx 8rpx;
            font-size: 24rpx;
            color: #FB2A2C;
            margin-left: 16rpx;
        }
    }

    &_r {
        font-size: 26rpx;
    }
}

.order_card_content {
    width: 100%;
    border-bottom: 2rpx solid #F8F8F8;

    .order_id {
        font-size: 28rpx;
        color: #222222;
        margin-top: 24rpx;
    }

    .work,
    .address {
        width: 100%;
        min-height: 40rpx;
        display: flex;
        align-items: flex-start;
        margin-top: 24rpx;
    }

    ._icon {
        width: 38rpx;
        height: 38rpx;
        margin-right: 14rpx;

        image {
            width: 100%;
            height: 100%;
        }
    }

    ._text {
        width: calc(100% - 38rpx - 14rpx);
        font-size: 28rpx;
        color: #222222;
    }

    .time {
        width: 100%;
        min-height: 40rpx;
        // display: flex;
        // align-items: flex-start;
        margin: 26rpx 0;

        .t_l {
            display: flex;

            ._icon {
                width: 28rpx;
                height: 28rpx;
                margin-right: 20rpx;
            }

            ._text {
                width: fit-content;
                color: #888888;
            }
        }

        .t_r {
            display: flex;
            align-items: center;
            font-size: 30rpx;
            color: #888888;
            font-size: 26rpx;
            color: #222222;

            .money {
                font-size: 38rpx;
                color: #FF382E;
                margin-left: 16rpx;
            }
        }
    }
}

.order_card_foot {
    width: 100%;
    height: 102rpx;

    &_l {
        font-size: 30rpx;
        color: #888888;
    }

    &_r {
        width: fit-content;
        background: #FE6E04;
        border-radius: 35rpx 35rpx 35rpx 35rpx;
        font-size: 30rpx;
        color: #FFFFFF;
        padding: 12rpx 40rpx;
        letter-spacing: 0.1em;
    }
}
</style>